package dbc;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class ConnectionPool {
/*
    static List<Connection> cs = new ArrayList<Connection>();

    int size;

    public ConnectionPool(int size) {
        this.size = size;
        init();
    }

    //创建线程数
    public void init() {
        try {
            System.out.println("加载驱动...");
            Class.forName(getProperties("sqlClass"));
            for (int i = 0; i < size; i++) {
                Connection c = DriverManager
                        .getConnection(getProperties("url"), getProperties("dbUser"), getProperties("dbPwd"));
                cs.add(c);
            }
            System.out.println("连接成功...数据池数：" + size);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public synchronized Connection getConnection() {
        while (cs.isEmpty()) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Connection c = cs.remove(0);
        return c;
    }

    public synchronized void returnConnection(Connection c) {
        cs.add(c);
        this.notifyAll();
    }
	*/
	
    /**
     * 解决国际化和本地化问题
     * @param key
     * @return String
     */
    public static String getProperties(String key){
        ResourceBundle resourceBundle =ResourceBundle.getBundle("db");
        return resourceBundle.getString(key);
    }
    
	public ConnectionPool() {
		 try {
			Class.forName(getProperties("sqlClass"));
		} catch (ClassNotFoundException e) {
			System.out.println("驱动类异常：" +e.getMessage());
		}
	}
	
    public Connection getConnection() {
        Connection c = null;
		try {
			Class.forName(getProperties("sqlClass"));
			c = DriverManager
					.getConnection(getProperties("url"), getProperties("dbUser"), getProperties("dbPwd"));
			//System.out.println("数据库连接成功：" + c);
		} catch (SQLException e) {
			System.out.println("数据库连接异常:" + e.getMessage());
		} catch (ClassNotFoundException e) {
			System.out.println("驱动类异常：" +e.getMessage());
		}
        return c;
    }
    
    public  void returnConnection(Connection c) {
       try {
		c.close();
		//System.out.println("连接关闭成功");
	} catch (SQLException e) {
		System.out.println("数据库关闭异常:" + e.getMessage());
	}
    }
}